package com.unigator;

import java.util.ArrayList;
import java.util.HashMap;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.model.LatLng;
import com.unigator.MainActivity.transData;
import com.unigator.ServerIO.RetObject;
import com.unigator.ServerIO.eFunc;

import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;

public class RecordPath extends Activity implements LocationListener 
{

	private int startId;
	private ArrayList<Double[]> edge;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_record_path);
		edge=new ArrayList<Double[]>();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.record_path, menu);
		return true;
	}

	public void start(View view) 
    {
		// Getting LocationManager object from System Service LOCATION_SERVICE
        LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

        // Creating a criteria object to retrieve provider
        Criteria criteria = new Criteria();

        // Getting the name of the best provider
        String provider = locationManager.getBestProvider(criteria, true);

        // Getting Current Location
        Location location = locationManager.getLastKnownLocation(provider);
        
        new transData().execute(eFunc.AddVertex,location.getLatitude(),location.getLongitude());
        
        locationManager.requestLocationUpdates(provider, 2000, 0, this);
        
        if(location!=null){
        	onLocationChanged(location);
        }
    }
	
	@Override
    public void onLocationChanged(Location location) 
	{
        // Getting latitude of the current location
		double latitude = location.getLatitude();
        // Getting longitude of the current location
        double longitude = location.getLongitude();
        
        Double [] arr =new Double [2];
        arr[0]=latitude;
        arr[1]=longitude;
        edge.add(arr);
        
    }
	
	public void pause(View view) 
    {
		new transData().execute(eFunc.AddEdge,edge);
		edge.clear();
    }
	
	public void stop(View view) 
    {
		new transData().execute(eFunc.AddEdge,edge);
		edge.clear();
		Intent intent = new Intent(this, MainActivity.class);
	    startActivity(intent);
    }

	@Override
	public void onProviderDisabled(String arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onProviderEnabled(String arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
		// TODO Auto-generated method stub
		
	}
	
	public class transData extends ServerIO
    {
    	transData()
    	{
    		super();
    	}
    	
    	@Override
    	protected void onPostExecute(Object object)
    	{
    		RetObject retObject=(RetObject)object;
    		eFunc func=retObject.func;
    		switch(func)
    		{
    			case AddVertex:
    			{
    				startId=(Integer)retObject.retData;
    				break;
    			}
    			case AddEdge:
    			{
    				break;
    			}
    		}
    		
    	}
    }
}
